package com.xmtlxx.common.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.xmtlxx.common.core.domain.entity.SysUser;
import com.xmtlxx.common.core.domain.model.LoginUser;
import com.xmtlxx.common.exception.ServiceException;
import com.xmtlxx.common.helper.LoginHelper;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
 * 安全服务工具类
 *
 * @author ruoyi
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SecurityUtils {

    /**
     * 用户ID
     **/
    public static Long getUserId() {
        try {
            return getLoginUser().getUserId();
        } catch (Exception e) {
            throw new ServiceException("获取用户ID异常" , HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 获取部门ID
     **/
    public static Long getDeptId() {
        try {
            return getLoginUser().getDeptId();
        } catch (Exception e) {
            throw new ServiceException("获取部门ID异常" , HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 获取用户账户
     **/
    public static String getUsername() {
        try {
            return getLoginUser().getUsername();
        } catch (Exception e) {
            throw new ServiceException("获取用户账户异常" , HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 获取用户对应租户ID
     **/
    public static Long getTenantId() {
        try {
            return getLoginUser().getTenantId();
        } catch (Exception e) {
            throw new ServiceException("获取用户租户信息异常" , HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 获取用户对应父级租户ID
     **/
    public static Long getTenantPid() {
        try {
            return getLoginUser().getTenantPid();
        } catch (Exception e) {
            throw new ServiceException("获取用户租户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }


    /**
     * 获取用户对应租户名称
     **/
    public static String getTenantName() {
        try {
            return getLoginUser().getTenantName();
        } catch (Exception e) {
            throw new ServiceException("获取用户租户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        try {
            return LoginHelper.getLoginUser();
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常" , HttpStatus.HTTP_UNAUTHORIZED);
        }
    }

    /**
     * 是否为平台超级管理员
     *
     * @return 结果
     */
    public static boolean isSuperAdmin(Long userId) {
        return userId != null && 1L == userId;
    }

    /**
     * 是否为平台超级管理员
     *
     * @return 结果
     */
    public static boolean isSuperAdmin() {
        return 1L == getLoginUser().getUserId();
    }

    /**
     * 是否为租户管理员
     *
     * @return 结果
     */
    public static boolean isTenantAdmin(SysUser user) {
        return user.getUserType().equals("01");
    }

    /**
     * 是否为系统用户
     *
     * @return 结果
     */
    public static boolean isSystemUser() {
        return getLoginUser().getUserType().equals("00");
    }

    /**
     * 是否为租户管理员
     *
     * @return 结果
     */
    public static boolean isTenantAdmin() {
        return getLoginUser().getUserType().equals("01");
    }

    /**
     * 是否为管理后台用户
     */
    public static boolean isPlatformTenant() {
        return ObjectUtil.equals(SecurityUtils.getTenantId(), 1);
    }
}
